<?php

class Option extends AppModel {

    var $alias = 'Option';
    var $name = 'Option';
    var $useTable = 'poll_options';
    var $belongsTo = array(
        'Poll' => array(
            'className' => 'Poll',
            'foreignKey' => 'poll_id',
            'fields' => array('Poll.id', 'Poll.title', 'Poll.alias')
        )
    );

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }
    function writePoll($poll_id = 1){
        $data = $this->find('all', array(
                    'conditions' => array('Option.published' => '1', 'Option.poll_id' => $poll_id),
                    'fields' => array('Option.id', 'Option.url', 'Option.title'),
                    'order' => array(
                        'Option.ordering' => 'ASC'
                    ),
                    'offset'    =>  0
        ));
        $html   =   '';
        $i = 1;
        foreach($data AS $row){
            $html   .=  '<p class="poll-row">';
            $html   .=  '<input type="radio" name="poll-name" value="' . $row['Option']['id'] . '" id="option-vote-' . $row['Option']['id'] . '"/>';
            $html   .=  '<label for="option-vote-' . $row['Option']['id'] . '"><a target="_blank" href="' . $row['Option']['url'] . '" title="' . $row['Option']['title'] . '">' . $row['Option']['title'] . '</a></label>';
            $html   .=  '</p>';
        }
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'poll_games.ctp', 'w');
        fwrite($resourse, $html);
    }
}

?>
